import { onMounted, onUnmounted, ref } from "vue";
import { useTime } from "@/hooks/useTime";

/**
 * 页面倒计时功能
 */
export const useDaoJiShi = () => {
  let currentTime = ref<any>();
  let timer: any = null;

  /**
   * 清除定时器
   */
  const clearSetInterval = () => {
    timer && clearInterval(timer);
  };

  /**
   * 创建定时器
   */
  const ceatedSetInterval = () => {
    timer = setInterval(() => {
      const { nowTime } = useTime();
      currentTime.value = nowTime.value;
      console.log("每1秒执行一次", currentTime);
    }, 1000);
  };

  onMounted(() => {
    ceatedSetInterval();
  });
  onUnmounted(() => {
    clearSetInterval();
  });

  return {
    currentTime
  };
};
